using Boo.Lang;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

[Serializable]
public class RuntimeTerrains : MonoBehaviour
{
	[Serializable]
	[CompilerGenerated]
	internal sealed class _0024GenerateUpdate_0024789 /*: GenericGenerator<object>*/
	{
		internal RuntimeTerrains _0024self__0024791;

		public _0024GenerateUpdate_0024789(RuntimeTerrains self_)
		{
			_0024self__0024791 = self_;
		}

        //public override IEnumerator<object> GetEnumerator()
        //{
        //	return new _0024(_0024self__0024791);
        //}
        public IEnumerator SubtractNewPlatformVelocity()
        {
            // Э���߼�
            yield return new WaitForSeconds(1f);
        }
    }

	[Serializable]
	[CompilerGenerated]
	internal sealed class _0024GenerateStart_0024792 /*: GenericGenerator<object>*/
	{
		internal RuntimeTerrains _0024self__0024794;

		public _0024GenerateStart_0024792(RuntimeTerrains self_)
		{
			_0024self__0024794 = self_;
		}

        //public override IEnumerator<object> GetEnumerator()
        //{
        //	return new _0024(_0024self__0024794);
        //}
        public IEnumerator SubtractNewPlatformVelocity()
        {
            // Э���߼�
            yield return new WaitForSeconds(1f);
        }
    }

	private GameObject TerrainComposer;

	private terraincomposer_save tc_script;

	private GameObject TerrainComposerClone;

	private terraincomposer_save tc_script2;

	private float frames;

	private int currentOutput;

	public bool moveTerrainsWithCamera;

	public float updateCheckTime;

	public Transform mainCamera;

	public bool generateOnStart;

	public bool createTerrainsOnTheFly;

	public bool autoSpeed;

	public int targetFrames;

	public int generateSpeed;

	public bool heightmapOutput;

	public bool splatOutput;

	public bool treeOutput;

	public bool grassOutput;

	public bool objectOutput;

	public int seed;

	public bool randomSeed;

	public bool randomizeHeightmapOutput;

	public Vector2 randomizeHeightmapRange;

	public bool randomizeTreeOutput;

	public Vector2 randomizeTreeRange;

	public bool randomizeGrassOutput;

	public Vector2 randomizeGrassRange;

	public bool randomizeObjectOutput;

	public Vector2 randomizeObjectRange;

	private float terrainSize;

	private float totalSize;

	private Vector3[] initPos;

	private Vector3 oldPos;

	private float terrainSizeHalf;

	private float relativePos;

	private float newPos;

	private float offset;

	private GUIStyle myStyle;

	private System.Collections.Generic.List<terrain_class> terrains;

	private bool resetActive;

	private bool generateStart;

	private bool generating;

	public RuntimeTerrains()
	{
		updateCheckTime = 0.5f;
		generateOnStart = true;
		autoSpeed = true;
		targetFrames = 90;
		generateSpeed = 500;
		seed = 10;
		randomizeHeightmapRange = new Vector2(0f, 1000f);
		randomizeTreeRange = new Vector2(0f, 1000f);
		randomizeGrassRange = new Vector2(0f, 1000f);
		randomizeObjectRange = new Vector2(0f, 1000f);
		terrains = new System.Collections.Generic.List<terrain_class>();
	}

	public void Start()
	{
		TerrainComposer = GameObject.Find("TerrainComposer_Save");
		tc_script = (terraincomposer_save)TerrainComposer.GetComponent(typeof(terraincomposer_save));
		tc_script.heightmap_output = false;
		tc_script.splat_output = false;
		myStyle = new GUIStyle();
		if (createTerrainsOnTheFly)
		{
			CreateTerrains();
		}
		if (moveTerrainsWithCamera)
		{
			if (mainCamera == null)
			{
				UnityEngine.Debug.Log("Assign the Main Camera");
				UnityEngine.Debug.Break();
			}
			StartMoveTerrains();
			InvokeRepeating("UpdateMoveTerrain", 0f, updateCheckTime);
		}
		if (generateOnStart)
		{
			StartCoroutine(GenerateStart());
		}
	}

	public void CreateTerrains()
	{
		tc_script.create_terrain(tc_script.terrains[0], tc_script.terrainTiles);
		tc_script.fit_terrain_tiles(tc_script.terrains[0], refit: true);
		ParentTerrains();
	}

	public void UpdateMoveTerrain()
	{
		if (!generating)
		{
			resetActive = false;
			generateStart = false;
			ResetActive();
			UpdateTerrainPositionsX();
			UpdateTerrainPositionsZ();
			if (generateStart)
			{
				StartCoroutine(GenerateStart());
			}
		}
	}

	public void ResetActive()
	{
		if (!resetActive)
		{
			for (int i = 0; i < tc_script.terrains.Count; i++)
			{
				tc_script.terrains[i].active = false;
			}
			resetActive = true;
		}
	}

	public IEnumerator GenerateUpdate()
	{
        //return new _0024GenerateUpdate_0024789(this).GetEnumerator();
        yield return new WaitForSeconds(1f);
    }

	public IEnumerator GenerateStart()
	{
        //return new _0024GenerateStart_0024792(this).GetEnumerator();
        yield return new WaitForSeconds(1f);
    }

	public void GenerateStop()
	{
		UnityEngine.Object.Destroy(TerrainComposerClone);
		if (currentOutput == 5)
		{
			for (int i = 0; i < terrains.Count; i++)
			{
				terrains[i].terrain.gameObject.SetActive(value: true);
			}
			terrains.Clear();
			generating = false;
		}
	}

	public bool SelectCloneOutput()
	{
		tc_script.disable_outputs();
		if (randomSeed)
		{
			seed = (int)(Time.realtimeSinceStartup * 2000f);
		}
		int result;
		if (currentOutput == 0)
		{
			currentOutput = 1;
			if (heightmapOutput)
			{
				tc_script.heightmap_output = true;
				CreateClone();
				if (randomizeHeightmapOutput)
				{
					tc_script2.randomize_layer_offset(layer_output_enum.heightmap, randomizeHeightmapRange, seed);
				}
				result = 1;
				goto IL_017b;
			}
		}
		if (currentOutput == 1)
		{
			tc_script.terrains[0].SetAllNeighbors(tc_script.terrains);
			currentOutput = 2;
			if (splatOutput)
			{
				tc_script.splat_output = true;
				CreateClone();
				result = 1;
				goto IL_017b;
			}
		}
		if (currentOutput == 2)
		{
			currentOutput = 3;
			if (treeOutput)
			{
				tc_script.tree_output = true;
				CreateClone();
				result = 1;
				goto IL_017b;
			}
		}
		if (currentOutput == 3)
		{
			currentOutput = 4;
			if (grassOutput)
			{
				tc_script.grass_output = true;
				CreateClone();
				result = 1;
				goto IL_017b;
			}
		}
		if (currentOutput == 4)
		{
			currentOutput = 5;
			if (objectOutput)
			{
				tc_script.object_output = true;
				CreateClone();
				result = 1;
				goto IL_017b;
			}
		}
		result = 0;
		goto IL_017b;
		IL_017b:
		return (byte)result != 0;
	}

	public void CreateClone()
	{
		if ((bool)tc_script2)
		{
			GenerateStop();
		}
		TerrainComposerClone = UnityEngine.Object.Instantiate(TerrainComposer);
		TerrainComposerClone.name = "<Generating>";
		tc_script2 = (terraincomposer_save)TerrainComposerClone.GetComponent(typeof(terraincomposer_save));
		tc_script2.script_base = tc_script;
		tc_script2.auto_speed = autoSpeed;
		tc_script2.generate_speed = generateSpeed;
		tc_script2.target_frame = targetFrames;
		tc_script2.runtime = true;
		tc_script2.generate_begin();
	}

	public void TerrainsFlush()
	{
		for (int i = 0; i < tc_script.terrains.Count; i++)
		{
			tc_script.terrains[i].terrain.Flush();
		}
	}

	public void ParentTerrains()
	{
		GameObject gameObject = new GameObject();
		gameObject.name = "_Terrains";
		for (int i = 0; i < tc_script.terrains.Count; i++)
		{
			tc_script.terrains[i].terrain.transform.parent = gameObject.transform;
		}
	}

	public void StartMoveTerrains()
	{
		terrainSize = tc_script.terrains[0].size.x;
		totalSize = tc_script.terrains[0].tiles.x * terrainSize;
		terrainSizeHalf = terrainSize / 2f;
		initPos = new Vector3[tc_script.terrains.Count];
		for (int i = 0; i < tc_script.terrains.Count; i++)
		{
			initPos[i] = tc_script.terrains[i].terrain.transform.position;
		}
		offset = terrainSize / 2f;
		if (totalSize / terrainSize % 2f != 0f)
		{
		}
		UpdateTerrainPositionsX();
		UpdateTerrainPositionsZ();
		if (generateStart)
		{
			StartCoroutine(GenerateStart());
		}
	}

	public void UpdateTerrainPositionsX()
	{
		for (int i = 0; i < tc_script.terrains.Count; i++)
		{
			Vector3 position = mainCamera.transform.position;
			relativePos = position.x - initPos[i].x;
			newPos = Mathf.Round((relativePos - offset) / totalSize) * totalSize + initPos[i].x;
			Vector3 position2 = tc_script.terrains[i].terrain.transform.position;
			if (position2.x != newPos)
			{
				Transform transform = tc_script.terrains[i].terrain.transform;
				float x = newPos;
				Vector3 position3 = tc_script.terrains[i].terrain.transform.position;
				float y = position3.y;
				Vector3 position4 = tc_script.terrains[i].terrain.transform.position;
				transform.position = new Vector3(x, y, position4.z);
				tc_script.terrains[i].terrain.gameObject.SetActive(value: false);
				terrains.Add(tc_script.terrains[i]);
				tc_script.terrains[i].active = true;
				generateStart = true;
			}
		}
		ref Vector3 reference = ref oldPos;
		Vector3 position5 = mainCamera.transform.position;
		reference.x = Mathf.Round(position5.x / terrainSize) * terrainSize;
	}

	public void UpdateTerrainPositionsZ()
	{
		for (int i = 0; i < tc_script.terrains.Count; i++)
		{
			Vector3 position = mainCamera.transform.position;
			relativePos = position.z - initPos[i].z;
			newPos = Mathf.Round((relativePos - offset) / totalSize) * totalSize + initPos[i].z;
			Vector3 position2 = tc_script.terrains[i].terrain.transform.position;
			if (position2.z != newPos)
			{
				Transform transform = tc_script.terrains[i].terrain.transform;
				Vector3 position3 = tc_script.terrains[i].terrain.transform.position;
				float x = position3.x;
				Vector3 position4 = tc_script.terrains[i].terrain.transform.position;
				transform.position = new Vector3(x, position4.y, newPos);
				tc_script.terrains[i].terrain.gameObject.SetActive(value: false);
				tc_script.terrains[i].SetAllNeighbors(tc_script.terrains);
				terrains.Add(tc_script.terrains[i]);
				tc_script.terrains[i].active = true;
				generateStart = true;
			}
		}
		ref Vector3 reference = ref oldPos;
		Vector3 position5 = mainCamera.transform.position;
		reference.z = Mathf.Round(position5.z / terrainSize) * terrainSize;
	}

	public void Main()
	{
	}
}
